package com.example.albumphoto

import android.view.GestureDetector.OnDoubleTapListener
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.github.chrisbanes.photoview.PhotoView

class PhotoPagerAdapter(private val photos: List<Int>) : RecyclerView.Adapter<PhotoPagerAdapter.PhotoViewHolder>() {

    inner class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val photoView: PhotoView = itemView.findViewById(R.id.photoView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_photo_detail, parent, false)
        return PhotoViewHolder(view)
    }

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
        val photoId = photos[position]

        // 使用Glide加载图片
        holder.photoView.setImageResource(photoId)

        // 设置PhotoView属性
        holder.photoView.apply {
            setScaleType(ImageView.ScaleType.FIT_CENTER)

            // 设置双击缩放监听
            setOnDoubleTapListener(object : OnDoubleTapListener {
                override fun onDoubleTap(e: MotionEvent): Boolean {
                    if (scale > 1f) {
                        setScale(1f, e.x, e.y, true)
                    } else {
                        setScale(3f, e.x, e.y, true)
                    }
                    return true
                }

                override fun onDoubleTapEvent(e: MotionEvent): Boolean = true
                override fun onSingleTapConfirmed(e: MotionEvent): Boolean = true
            })
        }
    }

    override fun getItemCount(): Int = photos.size
}